Go back to the Preprocessing page. This link might be useful to keep track of the files created during the preprocessing.
Let us set some global options for all code chunks in this document.
knitr::opts_chunk$set(
message = FALSE, # Disable messages printed by R code chunks
warning = FALSE, # Disable warnings printed by R code chunks
echo = TRUE, # Show R code within code chunks in output
include = TRUE, # Include both R code and its results in output
eval = TRUE, # Evaluate R code chunks
cache = FALSE, # Enable caching of R code chunks for faster rendering
fig.align = "center",
out.width = "100%",
retina = 2,
error = TRUE,
collapse = FALSE
)
rm(list = ls())
set.seed(1982)# Install R-INLA package
# install.packages("INLA",repos = c(getOption("repos"),INLA ="https://inla.r-inla-download.org/R/testing"), dep = TRUE)
# Update R-INLA package
# inla.upgrade(testing = TRUE)
# Install inlabru package
# remotes::install_github("inlabru-org/inlabru", ref = "devel")
# Install rSPDE package
# remotes::install_github("davidbolin/rspde", ref = "devel")
# Install MetricGraph package
# remotes::install_github("davidbolin/metricgraph", ref = "devel")
library(INLA)
library(inlabru)
library(rSPDE)
library(MetricGraph)
library(plotly)
library(dplyr)
library(tidyr)
library(sf)
library(here) # here() starts from the home directory
library(rmarkdown)
library(grateful) # Cite all loaded packages
rm(list = ls()) # Clear the workspace
set.seed(1982) # Set seed for reproducibility# Function to remove consecutive zeros
remove_consecutive_zeros <- function(vec) {
# Initialize a result vector
result <- numeric(length(vec))
# Index for the result vector
index <- 1
# Flag to track if the first zero has been encountered
first_zero <- FALSE
# Loop through the original vector
for (i in 1:length(vec)) {
# If current value is not zero or previous value is not zero, or it's the first zero, add it to result
if (vec[i] != 0 || (i > 1 && vec[i - 1] != 0) || i == 1) {
result[index] <- vec[i]
index <- index + 1
# Reset first_zero flag if it's the first zero
if (vec[i] == 0 && !first_zero) {
first_zero <- TRUE
}
} else {
# Replace consecutive zeros with NA after the first zero
result[index] <- NA
index <- index + 1
}
}
# Trim the result vector to remove unused entries
result <- result[1:(index - 1)]
return(result)
}# Choose the days for the analysis
days <- c(7,14,21,28) # every Thursday of January 2021
# Filter by days and hour of interest
aux <- january %>%
filter(day %in% days, hour %in% c(13)) %>% # Keep observations between 13:00 and 14:00
dplyr::select(-PDT, -hour) # Remove PDT and hour variables
# Get the unique buses ID
buses_ID <- unique(aux$ID)
# Remove more than one consecutive zeros from the speed variable and store the data in a new data frame df
df <- aux %>%
filter(ID == buses_ID[1], day == days[1]) %>% # Get the first bus in the first day
arrange(datetime) %>% # Arrange by datetime so that we can remove consecutive zeros
mutate(speed = remove_consecutive_zeros(speed)) %>% # Remove consecutive zeros
drop_na(speed) # Drop NAs, if any
for (i in 1:length(buses_ID)) { # Loop through the buses
for (j in 1:length(days)) { # Loop through the days
if (i == 1 && j == 1) { # Skip the first bus in the first day, as it has been processed already
next
}
check <- aux %>% filter(ID == buses_ID[i], day == days[j]) # Get the data for the bus and day
if(nrow(check) > 0){ # If there is data, process as before and append to df
tmp <- check %>%
arrange(datetime) %>%
mutate(speed = remove_consecutive_zeros(speed)) %>%
drop_na(speed)
df <- rbind(tmp, df)
}
}
}
# Change the days from days to 1,2,3, and 4 to make it easier to work with
newdays <- 1:4
df$day <- newdays[match(df$day, days)]
# Save the data corresponding to 7,14,21, and 28 January 2021 during 13:00-14:00 with no consecutive zeros in the speed variable
save(df, file = here("data_files/day7142128hour13noconsecutivezeroes.RData"))# Choose a bus ID
IDnumber <- 6697
# Get the data with all zeros for the chosen bus ID
bus <- january %>%
filter(day == 7, ID == IDnumber, hour == 13) %>%
arrange(datetime) %>%
dplyr::select(datetime, speed)
# Plot the speed records for the chosen bus ID
TSstudio::ts_plot(bus,
line.mode = "lines+markers",
title = paste("Speed records, all zeros, 7 January, 1-2pm, bus ID: ", IDnumber, sep = ""),
Xtitle = "Time",
Ytitle = "Speed")# Get the data with no consecutive zeros for the chosen bus ID
bus <- january %>%
filter(day == 7, ID == IDnumber, hour == 13) %>%
arrange(datetime) %>%
mutate(speed = remove_consecutive_zeros(speed)) %>%
drop_na(speed) %>%
dplyr::select(datetime, speed)
# Plot the speed records for the chosen bus ID
TSstudio::ts_plot(bus,
line.mode = "lines+markers",
title = paste("Speed records, no consecutive zeros, 7 January, 1-2pm, bus ID: ", IDnumber, sep = ""),
Xtitle = "Time",
Ytitle = "Speed")# Get the data with no consecutive zeros for the chosen bus ID (now from the new data frame df to check if it is the same)
bus <- df %>%
filter(day == 1, ID == IDnumber) %>% # Note that day 1 in df corresponds to 7 in january
arrange(datetime) %>%
dplyr::select(datetime, speed)
# Plot the speed records for the chosen bus ID
TSstudio::ts_plot(bus,
line.mode = "lines+markers",
title = paste("Speed records, no consecutive zeros, 7 January, 1-2pm, bus ID: ", IDnumber, sep = ""),
Xtitle = "Time",
Ytitle = "Speed")We used R version 4.4.0 (R Core Team 2024) and the following R packages: here v. 1.0.1 (Müller 2020), htmltools v. 0.5.8.1 (Cheng et al. 2024), INLA v. 24.6.27 (Rue, Martino, and Chopin 2009; Lindgren, Rue, and Lindström 2011; Martins et al. 2013; Lindgren and Rue 2015; De Coninck et al. 2016; Rue et al. 2017; Verbosio et al. 2017; Bakka et al. 2018; Kourounis, Fuchs, and Schenk 2018), inlabru v. 2.10.1.9010 (Yuan et al. 2017; Bachl et al. 2019), knitr v. 1.47 (Xie 2014, 2015, 2024), listviewer v. 4.0.0 (de Jong, Gainer, and Russell 2023), mapview v. 2.11.2 (Appelhans et al. 2023), MetricGraph v. 1.3.0.9000 (Bolin, Simas, and Wallin 2023b, 2023a, 2023c, 2024; Bolin et al. 2023), patchwork v. 1.2.0 (Pedersen 2024), plotly v. 4.10.4 (Sievert 2020), rmarkdown v. 2.27 (Xie, Allaire, and Grolemund 2018; Xie, Dervieux, and Riederer 2020; Allaire et al. 2024), rSPDE v. 2.3.3.9000 (Bolin and Kirchner 2020; Bolin and Simas 2023; Bolin, Simas, and Xiong 2023), scales v. 1.3.0 (Wickham, Pedersen, and Seidel 2023), sf v. 1.0.16 (Pebesma 2018; Pebesma and Bivand 2023), tidyverse v. 2.0.0 (Wickham et al. 2019), TSstudio v. 0.1.7 (Krispin 2023), xaringanExtra v. 0.8.0 (Aden-Buie and Warkentin 2024).